#include "LinuxNetRateObject.h"

#include <QtMath>

LinuxNetRateObject::LinuxNetRateObject(QObject *parent) : QObject(parent) {
    
}


QString LinuxNetRateObject::setRateUnitSensitive(LinuxNetRateObject::RateUnit unit, LinuxNetRateObject::Sensitive sensitive)
{
    switch (sensitive) {
    case Sensitive::Default: {
        switch (unit) {
        case RateUnit::RateBit:
            return QString("b/s");
        case RateUnit::RateByte:
            return QString("B/s");
        case RateUnit::RateKb:
            return QString("Kb/s");
        case RateUnit::RateMb:
            return QString("Mb/s");
        case RateUnit::RateGb:
            return QString("Gb/s");
        case RateUnit::RateTb:
            return QString("Tb/s");
        default:
//            qDebug()<<QString("Sensitive::Default,  RateUnit is RateUnknow.");
            return QString("");
        }
    }
    case Sensitive::Upper: {
        switch (unit) {
        case RateUnit::RateBit:
            return QString("BIT/S");
        case RateUnit::RateByte:
            return QString("B/S");
        case RateUnit::RateKb:
            return QString("KB/S");
        case RateUnit::RateMb:
            return QString("MB/S");
        case RateUnit::RateGb:
            return QString("GB/S");
        case RateUnit::RateTb:
            return QString("TB/S");
        default:
//            qDebug()<<QString("Sensitive::Upper,  RateUnit is RateUnknow.");
            return QString("");
        }
    }
    case Sensitive::Lower: {
        switch (unit) {
        case RateUnit::RateBit:
            return QString("bit/s");
        case RateUnit::RateByte:
            return QString("b/s");
        case RateUnit::RateKb:
            return QString("kb/s");
        case RateUnit::RateMb:
            return QString("mb/s");
        case RateUnit::RateGb:
            return QString("gb/s");
        case RateUnit::RateTb:
            return QString("tb/s");
        default:
//            qDebug()<<QString("Sensitive::Lower,  RateUnit is RateUnknow.");
            return QString("");
        }
    }
    default: {
//        qDebug()<<QString("Sensitive is RateUnknow.");
        return QString("");
    }
    }
}

double LinuxNetRateObject::autoRateUnits(long speed, LinuxNetRateObject::RateUnit &unit)
{
    /* 自动判断合适的速率单位,默认传进来的是 Byte
     * bit    0 ~ 7 位 (不到 1 字节)
     * Byte   1    ~ 2^10  Byte
     * KB     2^10 ~ 2^20  Byte
     * MB     2^20 ~ 2^30  Byte
     * GB     2^30 ~ 2^40  Byte
     * TB     2^40 ~ 2^50  Byte
     */

    if (unit != LinuxNetRateObject::RateByte) {
//        qDebug()<<"请先将单位转为字节(byte)后再传参";
        return -1;
    }

    double sp = 0;
    if (0 <= speed && speed < qPow(2, 10)) {
        unit = LinuxNetRateObject::RateByte;
        sp = speed;
    } else if (qPow(2, 10) <= speed && speed < qPow(2, 20)) {
        unit = LinuxNetRateObject::RateKb;
        sp = static_cast<double>(speed / qPow(2, 10) * 1.0);
    } else if (qPow(2, 20) <= speed && speed < qPow(2, 30)) {
        unit = LinuxNetRateObject::RateMb;
        sp = static_cast<double>(speed / qPow(2, 20) * 1.0);
    } else if (qPow(2, 30) <= speed && speed < qPow(2, 40)) {
        unit = LinuxNetRateObject::RateGb;
        sp = static_cast<double>(speed / qPow(2, 30) * 1.0);
    } else if (qPow(2, 40) <= speed && speed < qPow(2, 50)) {
        unit = LinuxNetRateObject::RateTb;
        sp = static_cast<double>(speed / qPow(2, 40) * 1.0);
    } else {
        unit = LinuxNetRateObject::RateUnknow;
//        qDebug()<<"本设备网络速率单位传输超过 TB, 或者低于 0 Byte.";
        sp = -1;
    }

    return sp;
}

QString LinuxNetRateObject::GetStringRateUnit(long speed) {
    LinuxNetRateObject::RateUnit unit = LinuxNetRateObject::RateByte;
    double new_rate = autoRateUnits(speed, unit);
    
    // 单位/s
    QString sens = setRateUnitSensitive(unit, LinuxNetRateObject::Lower);

    return QString("%1 %2").arg(new_rate, 0, 'f', 2, QLatin1Char(' ')).arg(sens);
}
